package com.brayan.melinxa.gui.panels.shadowers;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;

public class TemplatePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JFileChooser dirChooser = new JFileChooser("/");
	private JTextField textFieldDirectory;

	public TemplatePanel() {
		setBackground(Color.WHITE);

		dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{524, 0};
		gridBagLayout.rowHeights = new int[]{118, 200, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
				
				JPanel panel_2 = new JPanel();
				panel_2.setBackground(Color.WHITE);
				GridBagConstraints gbc_panel_2 = new GridBagConstraints();
				gbc_panel_2.anchor = GridBagConstraints.NORTH;
				gbc_panel_2.fill = GridBagConstraints.HORIZONTAL;
				gbc_panel_2.insets = new Insets(0, 0, 5, 0);
				gbc_panel_2.gridx = 0;
				gbc_panel_2.gridy = 0;
				add(panel_2, gbc_panel_2);
				panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));
				
				JPanel panel_Top = new JPanel();
				panel_Top.setBackground(Color.WHITE);
				panel_2.add(panel_Top);
				
				Component verticalStrut = Box.createVerticalStrut(20);
				panel_Top.add(verticalStrut);
				
						JPanel panel_Content = new JPanel();
						panel_2.add(panel_Content);
						panel_Content.setBackground(Color.WHITE);
						panel_Content.setAlignmentY(Component.TOP_ALIGNMENT);
						GridBagLayout gbl_panel_Content = new GridBagLayout();
						gbl_panel_Content.columnWidths = new int[] { 0, 220, 0, 0 };
						gbl_panel_Content.rowHeights = new int[] { 23, 0, 0, 0, 0 };
						gbl_panel_Content.columnWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
						gbl_panel_Content.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
						panel_Content.setLayout(gbl_panel_Content);
						
								Component horizontalStrut = Box.createHorizontalStrut(20);
								GridBagConstraints gbc_horizontalStrut = new GridBagConstraints();
								gbc_horizontalStrut.insets = new Insets(0, 0, 5, 5);
								gbc_horizontalStrut.gridx = 0;
								gbc_horizontalStrut.gridy = 0;
								panel_Content.add(horizontalStrut, gbc_horizontalStrut);
								
										JLabel labelSelectDirectory = new JLabel("Please select the directory you wish to be shaded (source directory).");
										labelSelectDirectory.setAlignmentY(Component.TOP_ALIGNMENT);
										GridBagConstraints gbc_labelSelectDirectory = new GridBagConstraints();
										gbc_labelSelectDirectory.anchor = GridBagConstraints.WEST;
										gbc_labelSelectDirectory.insets = new Insets(0, 0, 5, 5);
										gbc_labelSelectDirectory.gridx = 1;
										gbc_labelSelectDirectory.gridy = 0;
										panel_Content.add(labelSelectDirectory, gbc_labelSelectDirectory);
										
												Component horizontalStrut_1 = Box.createHorizontalStrut(20);
												GridBagConstraints gbc_horizontalStrut_1 = new GridBagConstraints();
												gbc_horizontalStrut_1.insets = new Insets(0, 0, 5, 0);
												gbc_horizontalStrut_1.gridx = 2;
												gbc_horizontalStrut_1.gridy = 0;
												panel_Content.add(horizontalStrut_1, gbc_horizontalStrut_1);
												
														textFieldDirectory = new JTextField();
														GridBagConstraints gbc_textFieldDirectory = new GridBagConstraints();
														gbc_textFieldDirectory.fill = GridBagConstraints.HORIZONTAL;
														gbc_textFieldDirectory.anchor = GridBagConstraints.WEST;
														gbc_textFieldDirectory.insets = new Insets(0, 0, 5, 5);
														gbc_textFieldDirectory.gridx = 1;
														gbc_textFieldDirectory.gridy = 1;
														panel_Content.add(textFieldDirectory, gbc_textFieldDirectory);
														textFieldDirectory.setHorizontalAlignment(SwingConstants.LEFT);
														textFieldDirectory.setAlignmentY(Component.TOP_ALIGNMENT);
														textFieldDirectory.setColumns(10);
														
																JButton btnSelectDirectory = new JButton("select source directory");
																GridBagConstraints gbc_btnSelectDirectory = new GridBagConstraints();
																gbc_btnSelectDirectory.insets = new Insets(0, 0, 5, 5);
																gbc_btnSelectDirectory.anchor = GridBagConstraints.NORTHWEST;
																gbc_btnSelectDirectory.gridx = 1;
																gbc_btnSelectDirectory.gridy = 2;
																panel_Content.add(btnSelectDirectory, gbc_btnSelectDirectory);
																
																Component verticalStrut_1 = Box.createVerticalStrut(20);
																GridBagConstraints gbc_verticalStrut_1 = new GridBagConstraints();
																gbc_verticalStrut_1.insets = new Insets(0, 0, 0, 5);
																gbc_verticalStrut_1.gridx = 1;
																gbc_verticalStrut_1.gridy = 3;
																panel_Content.add(verticalStrut_1, gbc_verticalStrut_1);
																
																		btnSelectDirectory.addActionListener(new ActionListener() {
																			public void actionPerformed(ActionEvent e) {
																				openFileSelector();
																			}
																		});
		
				JPanel panel_Description = new JPanel();
				panel_Description.setBackground(Color.WHITE);
				GridBagConstraints gbc_panel_Description = new GridBagConstraints();
				gbc_panel_Description.fill = GridBagConstraints.BOTH;
				gbc_panel_Description.gridx = 0;
				gbc_panel_Description.gridy = 1;
				add(panel_Description, gbc_panel_Description);
				panel_Description.setLayout(new BoxLayout(panel_Description, BoxLayout.Y_AXIS));
				
						JSeparator separator = new JSeparator();
						panel_Description.add(separator);
						
						JPanel panel_1 = new JPanel();
						panel_1.setBackground(Color.WHITE);
						panel_Description.add(panel_1);
						GridBagLayout gbl_panel_1 = new GridBagLayout();
						gbl_panel_1.columnWidths = new int[]{10, 514, 0};
						gbl_panel_1.rowHeights = new int[]{112, 0};
						gbl_panel_1.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
						gbl_panel_1.rowWeights = new double[]{1.0, Double.MIN_VALUE};
						panel_1.setLayout(gbl_panel_1);
						
						JPanel panel_Graph = new JPanel();
						GridBagConstraints gbc_panel_Graph = new GridBagConstraints();
						gbc_panel_Graph.fill = GridBagConstraints.BOTH;
						gbc_panel_Graph.insets = new Insets(0, 0, 0, 5);
						gbc_panel_Graph.gridx = 0;
						gbc_panel_Graph.gridy = 0;
						panel_1.add(panel_Graph, gbc_panel_Graph);
						panel_Graph.setBackground(SystemColor.textHighlight);
						
						JPanel panel_Detail = new JPanel();
						GridBagConstraints gbc_panel_Detail = new GridBagConstraints();
						gbc_panel_Detail.fill = GridBagConstraints.BOTH;
						gbc_panel_Detail.gridx = 1;
						gbc_panel_Detail.gridy = 0;
						panel_1.add(panel_Detail, gbc_panel_Detail);
						panel_Detail.setBackground(Color.WHITE);
						GridBagLayout gbl_panel_Detail = new GridBagLayout();
						gbl_panel_Detail.columnWidths = new int[]{0, 0, 0, 0};
						gbl_panel_Detail.rowHeights = new int[]{0, 0, 0, 0};
						gbl_panel_Detail.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
						gbl_panel_Detail.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
						panel_Detail.setLayout(gbl_panel_Detail);
						
						JSeparator separator_1 = new JSeparator();
						GridBagConstraints gbc_separator_1 = new GridBagConstraints();
						gbc_separator_1.insets = new Insets(0, 0, 5, 5);
						gbc_separator_1.gridx = 1;
						gbc_separator_1.gridy = 0;
						panel_Detail.add(separator_1, gbc_separator_1);
						
						Component horizontalStrut_2 = Box.createHorizontalStrut(20);
						GridBagConstraints gbc_horizontalStrut_2 = new GridBagConstraints();
						gbc_horizontalStrut_2.insets = new Insets(0, 0, 5, 5);
						gbc_horizontalStrut_2.gridx = 0;
						gbc_horizontalStrut_2.gridy = 1;
						panel_Detail.add(horizontalStrut_2, gbc_horizontalStrut_2);
						
						JTextPane textPaneTitle = new JTextPane();
						textPaneTitle.setContentType("text/html");
						textPaneTitle.setEditable(false);
						textPaneTitle.setText("<b>Source Directory</b>");
						GridBagConstraints gbc_textPaneTitle = new GridBagConstraints();
						gbc_textPaneTitle.anchor = GridBagConstraints.WEST;
						gbc_textPaneTitle.insets = new Insets(0, 0, 5, 5);
						gbc_textPaneTitle.fill = GridBagConstraints.HORIZONTAL;
						gbc_textPaneTitle.gridx = 1;
						gbc_textPaneTitle.gridy = 1;
						panel_Detail.add(textPaneTitle, gbc_textPaneTitle);
						
						Component horizontalStrut_3 = Box.createHorizontalStrut(20);
						GridBagConstraints gbc_horizontalStrut_3 = new GridBagConstraints();
						gbc_horizontalStrut_3.insets = new Insets(0, 0, 5, 0);
						gbc_horizontalStrut_3.gridx = 2;
						gbc_horizontalStrut_3.gridy = 1;
						panel_Detail.add(horizontalStrut_3, gbc_horizontalStrut_3);
						
						JTextPane txtpnDescription = new JTextPane();
						txtpnDescription.setText("No other operation than \"read\" is invoked on this directory. Even if you select a wrong directory, no crucial operation is done. \r\n\r\nCaution! Focus on your personal data. Shading system files will hamper your system's responsivness.");
						GridBagConstraints gbc_txtpnDescription = new GridBagConstraints();
						gbc_txtpnDescription.insets = new Insets(0, 0, 0, 5);
						gbc_txtpnDescription.fill = GridBagConstraints.BOTH;
						gbc_txtpnDescription.gridx = 1;
						gbc_txtpnDescription.gridy = 2;
						panel_Detail.add(txtpnDescription, gbc_txtpnDescription);
		this.repaint();
	}

	private void openFileSelector() {
		// dirChooser.showDialog(this, "select directory");

		if (dirChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
			String path = dirChooser.getCurrentDirectory().getPath();
			try {
				textFieldDirectory.setText(dirChooser.getSelectedFile().getCanonicalPath());
			} catch (Exception ex) {
				System.out.println("Failed to evaluate canonical Path of the selected directory: "
						+ path);
			}
		} else {
			System.out.println("No Selection ");
		}
	}

}
